<template>
  <s-modal title="新增" :visible="visible" width="800px" ok-text="保存" :confirm-loading="confirmLoading"
    :closable="!confirmLoading" @ok="save()" @cancel="() => { if (!confirmLoading) { visible = false } }">
    <div style="height:400px">
      <s-scroll>
        <s-form ref="form" :value="formValue">
          <s-form-item label="日程详情" size="custom" name="tasksContent" :colon="true" :display-title="true"
            :options="{ 'labelWidth': 5, 'wrapperWidth': 12, 'offsetWidth': 1, 'total': 15 }"
            :rules="[{ 'max': 255, 'message': '最长为255位' }]">
            <s-input :allow-clear="true" placeholder="请输入任务详情" v-model="formValue.tasksContent" />
          </s-form-item>
          <s-form-item label="受邀人" size="custom" name="associate" :colon="true" :display-title="true"
            :options="{ 'labelWidth': 5, 'wrapperWidth': 12, 'offsetWidth': 1, 'total': 15 }">
            <s-select :allow-clear="true" placeholder="请选择受邀人" :show-search="true" mode="multiple" url="/realm/user/list"
              label-field="nickName" value-field="id" v-model="formValue.associate" />
          </s-form-item>
          <s-form-item label="执行时间" size="custom" name="createTime" :colon="true" :display-title="true"
            :options="{ 'labelWidth': 5, 'wrapperWidth': 12, 'offsetWidth': 1, 'total': 15 }">
            <s-date-picker :allow-clear="true" type="date" value-format="YYYY-MM-DD HH:mm:ss" :show-time="true"
              v-model="formValue.createTime" />
          </s-form-item>

        </s-form>
      </s-scroll>
    </div>
  </s-modal>
</template>

<script>

export default {
  name: 'Add',
  emits: ['change'],
  data() {
    return {
      formValue: {},
      confirmLoading: false,
      visible: false,
      userName: ''
    }
  },
  methods: {
    open() {
      this.formValue = {}
      this.visible = true
    },
    save() {
      const _this = this
      const form = this.$refs.form
      this.$api.GET('/realm/mine/info').then(res => {
        this.userName = res.data.nickName

        form.validate((values, fullValue) => {
          this.confirmLoading = true
          this.$api.PUT('/modules/doorCalendarTasks/insert', { ...values, nickName: this.userName }).then(res => {
            this.confirmLoading = false
            this.visible = false
            this.$emit('success');
            this.$message.info(res.msg)
            this.$emit('addCalendarData', this.formValue);
          }).catch(() => {
            this.confirmLoading = false
          })
        })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>